/*
 *  Player - One Hell of a Robot Server
 *  Copyright (C) 2000
 *     Brian Gerkey, Kasper Stoy, Richard Vaughan, & Andrew Howard
 *     Andrew Martignoni III
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/*
 * $Id: VPDetector.cc 8122 2011-07-24 13:27:16Z thjc $
 *
 */

/** @ingroup drivers */
/** @{ */
/** @defgroup driver_vpdetector vpdetector
@brief VPDetector

Vanishing point detection by using Canny filter and Hough transform

@par Compile-time dependencies

- OpenCV

@par Provides

- @ref interface_fiducial

@par Requires

- @ref interface_camera : camera device to get image data from

@par Configuration requests

- none

@par Configuration file options

- canny_min (int)
  - Default: 0
  - If set to 1, the blobfinder will output a testpattern of three blobs.



@par Example

@verbatim
driver
(
  name "vpdetector"
  provides ["fiducial:0"]
  requires ["camera:0"]
  canny_lowthresh 50
  canny_hithresh 100
  canny_window 3
)
@endverbatim

@author Alejandro Pustowka

*/

/** @} */


#include <assert.h>
#include <stdio.h>
#if !defined (WIN32)
  #include <unistd.h>  /* close(2),fcntl(2),getpid(2),usleep(3),execvp(3),fork(2)*/
#endif
#include <string.h>  /* for strncpy(3),memcpy(3) */
#include <stdlib.h>  /* for atexit(3),atoi(3) */
#include <pthread.h> /* for pthread stuff */

#include <libplayercore/playercore.h>

#include "./VanishPoint.h"

#include <opencv/cv.h>
#include <opencv/highgui.h>

typedef struct
{
  int lowThresh;
  int highThresh;
  int gWindow;
  
}canny_params_t;

typedef struct
{
  float minAngleV;
  float maxAngleV;
  float minAngleH;
  float maxAngleH;
  float minDiffAngle;
  
}line_classif_params_t;

typedef struct
{
  int method;
  int minLineLength;
  double rhoRes;
  double angleRes;
  double param1;
  double param2;
  int maxLines;  
  
}hough_params_t;

typedef struct
{
  canny_params_t canny;
  line_classif_params_t line;
  hough_params_t hough;
}vp_params_t;


class VPDetector: public ThreadedDriver
{

    player_devaddr_t mCameraAddr;
    Device*          mCameraDev;
    
    //class for detecting the vanishpoint
    VanishPoint*        mVP;
    
    //image from camera
    //uint8_t*         mImg;
    IplImage*         mImg;
    uint8_t*         mTmp;
    uint16_t         mWidth;
    uint16_t         mHeight;     // the image dimensions
    
    //configuration parameters
    vp_params_t vp_params;
    
    //fiducial data
    player_fiducial_data_t fdata;
    
    

    // print the vanishing point coordinates to the console
    void Print();

  public:

    // constructor
    VPDetector(ConfigFile* cf, int section);
    virtual ~VPDetector();
    
    // This method will be invoked on each incoming message
    virtual int ProcessMessage(QueuePointer & resp_queue,
                               player_msghdr * hdr,
                               void * data);
    virtual void Main();
    int MainSetup();
    void MainQuit();
    
    void ProcessImageData();
};
